package com.teamwings.modules.head.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.vo.DesignListResult;
import com.teamwings.common.vo.Result;
import com.teamwings.modules.head.entity.FormTableHead;
import com.teamwings.modules.head.service.IFormTableHeadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-05-06
 */
@RestController
@Api(tags = "表单head")
@RequestMapping("/form/head")
@Slf4j
public class HeadController {

    @Autowired
    private IFormTableHeadService formTableHeadService;

    /**
     * 保存表单head记录
     */
    @Caching(evict = {
            @CacheEvict(value = FormConstant.FORM_HEAD_PAGING, key = "'*'"),
    })
    @ApiOperation(value = "添加表单head数据", notes = "添加表单head数据")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody FormTableHead head) {
        formTableHeadService.saveMain(head,true,false);
        return Result.OK("添加成功！", head);
    }

    /**
     * 复制head数据
     * @param head /
     * @return /
     */
    @Caching(evict = {
            @CacheEvict(value = FormConstant.FORM_HEAD_COLUMN, key="#head.id"),
            @CacheEvict(value = FormConstant.FORM_HEAD_PAGING, key = "'*'"),
    })
    @ApiOperation(value = "复制表单head数据", notes = "复制表单head数据")
    @PostMapping(value = "/copy")
    public Result<?> copy(@RequestBody FormTableHead head) {
        formTableHeadService.copyMain(head);
        return Result.OK();
    }

    /**
     * 更新表单head记录
     */
    @Caching(evict = {
            @CacheEvict(value = FormConstant.FORM_HEAD_PAGING, key = "'*'"),
    })
    @ApiOperation(value = "编辑表单head数据", notes = "编辑表单head数据")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody FormTableHead head) {
        formTableHeadService.editMain(head);
        return Result.OK("修改成功！", head);
    }

    /**
     * 分页查询
     */
    @Cacheable(value = FormConstant.FORM_HEAD_PAGING, key="#page+'_'+#pageSize+'_'+#cateId+'_'+#formName")
    @ApiOperation(value = "获取表单head分页", notes = "获取表单head分页")
    @GetMapping(value = "/list")
    public Result<?> list(
                          @RequestParam(value = "pageNo", defaultValue = "1") Long page,
                          @RequestParam(value = "pageSize", defaultValue = "20") Long pageSize,
                          @RequestParam(value = "cateId" , required = false) String cateId,
                          @RequestParam(value = "formName" , required = false) String formName,
                          @RequestParam(value = "startDate" , required = false) String startDate,
                          @RequestParam(value = "endDate" , required = false) String endDate
    ) {
        Page<FormTableHead> catePage = new Page<>(page, pageSize);
//        LambdaQueryWrapper<FormTableHead> queryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<FormTableHead> queryWrapper = Wrappers.<FormTableHead>lambdaQuery()
                .orderByDesc(FormTableHead::getCreateTime);
        queryWrapper.eq(FormTableHead::getDeleted, 0);

        // 名称条件
        if (formName != null && !formName.isEmpty()) {
            queryWrapper.like(FormTableHead::getTableCname, formName);
        }

        // 分类条件
        if (cateId != null && !"0".equals(cateId)){
          queryWrapper.eq(FormTableHead::getTableCateId,cateId);
        }

        if (StringUtils.isNotEmpty(startDate)){
            queryWrapper.ge(FormTableHead::getCreateTime,startDate)
                .le(FormTableHead::getCreateTime,endDate);
        }
//        queryWrapper.orderByAsc(FormTableHead::getId);
        IPage<FormTableHead> list = formTableHeadService.getListWhitCode(catePage, queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation(value = "获取导出表单head", notes = "获取导出表单head")
    @GetMapping(value = "/exportList")
    public Result<?> exportList(
        @RequestParam(value = "startDate" , required = false) String startDate,
        @RequestParam(value = "endDate" , required = false) String endDate
    ) {
        Page<FormTableHead> catePage = new Page<>(1, 10000);
        LambdaQueryWrapper<FormTableHead> queryWrapper = Wrappers.<FormTableHead>lambdaQuery()
            .orderByDesc(FormTableHead::getUpdateTime);
        queryWrapper.eq(FormTableHead::getDeleted, 0);

        if (StringUtils.isNotEmpty(startDate)){
            queryWrapper.ge(FormTableHead::getUpdateTime,startDate)
                .le(FormTableHead::getUpdateTime,endDate);
        }
        IPage<FormTableHead> list = formTableHeadService.getListWhitCode(catePage, queryWrapper);
        return Result.OK(list);
    }

    /**
     * 根据ID查询表单head记录
     */
    @ApiOperation(value = "获取一条表单head数据", notes = "获取一条表单head数据")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        FormTableHead entity = formTableHeadService.getById(id);
        return Result.OK("获取数据成功！", entity);
    }


    /**
     * 根据ID删除表单head记录（DELETED = 1）
     */
    @Caching(evict = {
            @CacheEvict(value = {
                    FormConstant.FORM_HEAD_HEAD,
                    FormConstant.FORM_HEAD_COLUMN
            }, key="#id"),
            @CacheEvict(value = FormConstant.FORM_HEAD_PAGING, key = "'*'"),
    })
    @ApiOperation(value = "删除一条表单head数据", notes = "删除一条表单head数据")
    @DeleteMapping(value = "/removeById")
    public Result<?> removeById(@RequestParam(name = "id", required = true) String id) {
        formTableHeadService.removeById(id);
        return Result.OK("删除数据成功！");
    }

//    @ApiOperation(value = "测试外部数据源的设计列表", notes = "测试外部数据源的设计列表")
//    @GetMapping(value = "/ex")
//    public Result<?> ex(@RequestParam(value = "page", defaultValue = "1") Long page,
//                        @RequestParam(value = "pageSize", defaultValue = "10") Long pageSize) {
//        DesignListResult result = new DesignListResult();
//
//        ArrayList<String> strings = Lists.newArrayList( "tableName", "tableCname", "tableVersion");
//        ArrayList<String> strings2 = Lists.newArrayList( "表名", "表中文名", "表版本号");
//        Page<FormTableHead> page1 = formTableHeadService.page(new Page<>(page, pageSize),
//            new LambdaQueryWrapper<FormTableHead>()
//        );
//        result.setColumns(strings2)
//            .setColumnsEN(strings)
//            .setData(page1);
//
//        return Result.OK(result);
//    }

}
